package com.lz.topic;

import com.lz.RabbitMQConnector;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

/**
 * 生产者
 */
public class ProducerTopic {
    public static void main(String[] args) throws Exception {
        String exchangeName = "lz_exchange_topic";
        String queueName1 = "lz_queue_topic_1";
        String queueName2 = "lz_queue_topic_2";
        String queueName3 = "lz_queue_topic_3";
        String queueName4 = "lz_queue_topic_4";

        String routingKey1 = "key1.key2.*";
        String routingKey2 = "key1.*";
        String routingKey3 = "*.key2.*";
        String routingKey4 = "#";

        Channel channel = RabbitMQConnector.createConnectionAndChannel();

        /**
         * 创建交换机
         */
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true, false, null);

        /**
         * 创建队列
         */
        channel.queueDeclare(queueName1, true, false, false, null);
        channel.queueDeclare(queueName2, true, false, false, null);
        channel.queueDeclare(queueName3, true, false, false, null);
        channel.queueDeclare(queueName4, true, false, false, null);

        /**
         * 绑定队列和交换机
         * 1.队列名称
         * 2.交换机名称
         * 3.路由键，在直连模式下，可以为我们的队列名称
         */
        channel.queueBind(queueName1, exchangeName, routingKey1);
        channel.queueBind(queueName2, exchangeName, routingKey2);
        channel.queueBind(queueName3, exchangeName, routingKey3);
        channel.queueBind(queueName4, exchangeName, routingKey4);


        /**
         * 发送消息
         * 1.发送到哪个交换机
         * 2.路由键 routingKey
         * 3.其他参数信息
         * 4.消息主体
         */
        channel.basicPublish(exchangeName, "key1.key2.key3", null, "topic message".getBytes());
        System.out.println("发送消息成功");
        RabbitMQConnector.closeResources();
    }
}
